// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/dwio/common/selective_column_reader_internal.h>
#include <pollux/dwio/dwrf/common/decoder_util.h>
#include <pollux/dwio/dwrf/reader/dwrf_data.h>

namespace kumo::pollux::dwrf {

class SelectiveStringDictionaryColumnReader
    : public dwio::common::SelectiveColumnReader {
 public:
  using ValueType = int32_t;

  SelectiveStringDictionaryColumnReader(
      const std::shared_ptr<const dwio::common::TypeWithId>& fileType,
      DwrfParams& params,
      common::ScanSpec& scanSpec);

  bool hasBulkPath() const override {
    // Only ORC uses RLEv2 encoding. Currently, ORC string data does not
    // support fastpath reads. When reading RLEv2-encoded string data
    // with null, the query will fail.
    return version_ != pollux::dwrf::RleVersion_2;
  }

  void seekToRowGroup(int64_t index) override {
    SelectiveColumnReader::seekToRowGroup(index);
    auto positionsProvider = formatData_->as<DwrfData>().seekToRowGroup(index);
    if (strideDictStream_) {
      strideDictStream_->seekToPosition(positionsProvider);
      strideDictLengthDecoder_->seekToRowGroup(positionsProvider);
      // skip row group dictionary size
      positionsProvider.next();
    }

    dictIndex_->seekToRowGroup(positionsProvider);

    if (inDictionaryReader_) {
      inDictionaryReader_->seekToRowGroup(positionsProvider);
    }

    POLLUX_CHECK(!positionsProvider.hasNext());
  }

  uint64_t skip(uint64_t numValues) override;

  void read(int64_t offset, const RowSet& rows, const uint64_t* incomingNulls)
      override;

  void getValues(const RowSet& rows, VectorPtr* result) override;

 private:
  void loadStrideDictionary();
  void makeDictionaryBaseVector();

  template <typename TVisitor>
  void readWithVisitor(TVisitor visitor);

  // Fills 'values' from 'data' and 'lengthDecoder'. The count of
  // values is in 'values.numValues'.
  void loadDictionary(
      dwio::common::SeekableInputStream& data,
      dwio::common::IntDecoder</*isSigned*/ false>& lengthDecoder,
      dwio::common::DictionaryValues& values);
  void ensureInitialized();

  void makeFlat(VectorPtr* result);

  std::unique_ptr<dwio::common::IntDecoder</*isSigned*/ false>> dictIndex_;
  std::unique_ptr<ByteRleDecoder> inDictionaryReader_;
  std::unique_ptr<dwio::common::SeekableInputStream> strideDictStream_;
  std::unique_ptr<dwio::common::IntDecoder</*isSigned*/ false>>
      strideDictLengthDecoder_;

  FlatVectorPtr<StringView> dictionaryValues_;

  int64_t lastStrideIndex_;
  size_t positionOffset_;
  size_t strideDictSizeOffset_;
  RleVersion version_;

  const StrideIndexProvider& provider_;
  dwio::common::ColumnReaderStatistics& statistics_;

  // lazy load the dictionary
  std::unique_ptr<dwio::common::IntDecoder</*isSigned*/ false>> lengthDecoder_;
  std::unique_ptr<dwio::common::SeekableInputStream> blobStream_;
  bool initialized_{false};
  int64_t numRowsScanned_;
};

template <typename TVisitor>
void SelectiveStringDictionaryColumnReader::readWithVisitor(TVisitor visitor) {
  if (version_ == pollux::dwrf::RleVersion_1) {
    decodeWithVisitor<pollux::dwrf::RleDecoderV1<false>>(
        dictIndex_.get(), visitor);
  } else {
    decodeWithVisitor<pollux::dwrf::RleDecoderV2<false>>(
        dictIndex_.get(), visitor);
  }
}

} // namespace kumo::pollux::dwrf
